package com.article.controller;

import com.article.entity.dto.ArticleTypeDao;
import com.article.entity.dto.SearchArticleTypeDao;
import com.article.entity.pojo.ArticleType;
import com.article.entity.pojo.ColumnList;
import com.article.entity.vo.ArticleTypeVo;
import com.article.result.Result;
import com.article.service.ArtcleTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/articletype")
public class ArtcleTypeController {

    @Autowired
    private ArtcleTypeService artcleTypeService;

    @GetMapping("/getArticleType/{pageNo}/{limit}")
    public Result getArticleType(@PathVariable int pageNo, @PathVariable int limit) {
        log.info("查询文章类型，页码：{}，每页显示条数：{}", pageNo, limit);
        List<ArticleType> result = artcleTypeService.getArticleType(pageNo, limit);
        return Result.success(result);
    }

    @PostMapping("/addArticleType")
    public Result addArticleType(@RequestBody ArticleTypeDao articleTypeDao) {
        log.info("addArticleType:{}", articleTypeDao);
        articleTypeDao.setTypeTime(LocalDate.now().toString());
        int result = artcleTypeService.addArticleType(articleTypeDao);
        if (result == 1) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @DeleteMapping("/deleteArticleType/{id}")
    public Result deleteArticleType(@PathVariable(value = "id") Integer id) {
        int result = artcleTypeService.deleteArticleType(id);
        if (result == 1) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PostMapping("/deleteArticleTypes")
    public Result deleteArticleTypes(@RequestBody List<Integer> ids) {
        int result = artcleTypeService.deleteArticleTypes(ids);
        if (result == 1) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PutMapping("/updateArticleType")
    public Result updateArticleType(@RequestBody ArticleTypeDao articleTypeDao) {
        log.info("修改文章类型：{}", articleTypeDao);
        articleTypeDao.setTypeTime(LocalDate.now().toString());
        int result = artcleTypeService.updateArticleType(articleTypeDao);
        if (result == 1) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @PostMapping("/serachArticleType")
    public Result serachArticleType(@RequestBody SearchArticleTypeDao searchArticleTypeDao) {
        log.info("查询文章类型：{}", searchArticleTypeDao);
        List<ArticleTypeVo> articleTypeDaos = artcleTypeService.searchArticleType(searchArticleTypeDao);
        if (!articleTypeDaos.isEmpty()) {
            return Result.success(articleTypeDaos);
        }
        return Result.error("查询失败");
    }

    @GetMapping("/getArticleTypeNumber")
    public Result getArticleTypeNumber() {
        log.info("查询文章类型数量");
        int number = artcleTypeService.selectArticleTypeNumber();
        return Result.success(number);
    }
}
